<?php

defined('_JEXEC') or die( 'Restricted access' ); 
jimport('joomla.form.formfield');
class JFormFieldJvselection extends JFormField {

var	$type = 'sjselection';

function getInput(){
return JElementJvselection::fetchElement($this->name, $this->value, $this->element, $this->options['control']);
} 
} 
jimport('joomla.html.parameter.element');
class JElementJvselection extends JElement
{ 
var	$_name = 'sjselection';
function fetchElement($name, $value, &$node, $control_name){
$class = $node->attributes ( 'class' );
if (! $class) {
$class = "inputbox";
}
$db = &JFactory::getDBO();
$url = str_replace('administrator/','',JURI::base());
$downArrow = $url.'modules/mod_sj_tabs/assets/images/downarrow.png';
$upArrow = $url.'modules/mod_sj_tabs/assets/images/uparrow.png';
$cId = JRequest::getVar('cid','');		 
if($cId !='') $cId = $cId[0];
if($cId == ''){
$cId = JRequest::getVar('id');
}
$sql = "SELECT params FROM #__modules WHERE id=$cId";
$db->setQuery($sql);
$paramsConfigObj = $db->loadObjectList();
$db->setQuery($sql);
$data = $db->loadResult();
$params = new JParameter($data);
$sjType = $params->get('type','moduleID');				
$viewContent = $params->get('categoryID-view','word_count');
$selection = array();
$style = $params->get('st_tabs_style','sj_default');
$titlePos = $params->get('title_position','top');
$moduleSelected = $params->get('moduleID-list');
$categorySelected = $params->get('categoryID-list');
$k2Selected = $params->get('k2_category'); 
$arySelection = array();	
//Progress get module, content,k2
//Module
$query = 'SELECT a.*'
. ' FROM #__modules AS a'
. ' WHERE a.published = 1'			//TODO: rem it if don't want to check
. ' AND a.module <> \'mod_sj_tabs\''
//. ' ORDER BY a.id'
;

$db->setQuery($query);
$options = $db->loadObjectList();		
$id_str='';
$position_str='';
$modname_str='';
$i=0;		
foreach($options as $option){			
if ($i==0){
$mod_id_str = '\'module_'.$option->id . '\'';
$position_str = '\'' . $option->position . '\'';
$modtitle_str = '\''. $option->title . '\'';
}else{
$mod_id_str = $mod_id_str . ',' . '\'module_'. $option->id . '\'';
$position_str = $position_str . ',' . '"' . $option->position .'"';
$modtitle_str = $modtitle_str . ',' . '"' . $option->title . '"';
}
$i++;
}
//End module
//Content component 
$query = 'SELECT c.id, c.title' .
' FROM #__categories AS c' . 
' WHERE c.published = 1' .
' AND c.extension = \'com_content\' '.
' AND c.parent_id >0'.
' ORDER BY c.title'; 
$db->setQuery($query);
$options = $db->loadObjectList(); 
$id_str='';		
$catname_str='';
$i=0;
foreach($options as $option){
if ($i==0){
$category_id_str = '\'category_'.$option->id . '\'';
$cattitle_str = '\'' . $option->title . '\'';
}else{
$category_id_str = $category_id_str . ',' . '\'category_'. $option->id . '\'';
$cattitle_str = $cattitle_str . ',' . '"' . $option->title . '"';
}
$i++;
}
//End content 
//K2 component
$query = 'SELECT id,name as title 
FROM #__k2_categories m 
WHERE published=1 AND trash = 0 ORDER BY parent, ordering';
$db->setQuery($query);
$options = $db->loadObjectList(); 
$k2_id_str ='';
$catname_str='';
$i=0;
$k2cattitle_str = '';
if(count($options)){
foreach($options as $option){
if ($i==0){
$k2_id_str = '\'k2_'.$option->id . '\'';
$k2cattitle_str = '\'' . $option->title . '\'';
}else{
$k2_id_str = $k2_id_str . ',' . '\'k2_'. $option->id . '\'';
$k2cattitle_str = $k2cattitle_str . ',' . '"' . $option->title . '"';
}
$i++;
} 
}
//End k2
$baseURL = JURI::base();
$position = strrpos($baseURL,'administrator');
$url = substr($baseURL,0,$position);
//End
$html_return = "
<link href=\"".$url."modules/mod_sj_tabs/assets/adminstyle.css\" rel=\"stylesheet\" type=\"text/css\" />
<table width='95%' border='0' cellspacing='0' cellpadding='1' align='center' class='st_catagory_info'>
<tr><td>";
$html_return.= JHTML::_('select.genericlist',  $selection, 'st_selection', 'class="'.$class.'" MULTIPLE size="10"', 'id', 'title', $value, $control_name.$name );		
$html_return.="</td><td><table>
<tr><td>
<input class='button' type='button' value='Add' id ='list-selection-add' name='Add' onClick='addSelection();' />
</td></tr>
<tr><td>
<input class='button' type='button' value='Remove' id ='list-selection-remove' onClick='removeSelection();' name='Remove' />
</td></tr>
</table>
</td><td>";
$html_return.= JHTML::_('select.genericlist',  $arySelection, 'st_selection_selected', 'class="'.$class.'" MULTIPLE size="10" ', 'id', 'title', $value, 'st_selection_selected' );
$html_return.="</td><td style='padding:3px 0'>
<span id='st_up_arrow' style='cursor:pointer;'><img src='".$upArrow."' /></span><br />
<span id='st_down_arrow' style='cursor:pointer;margin-top:10px;'><img src='".$downArrow."' /></span>
</td></tr>
</table>";  
$html_return.="<script type=\"text/javascript\">			
var array_mod_id = new Array(".$mod_id_str.");
var array_mod_position = new Array(".$position_str.");
var array_mod_title = new Array(".$modtitle_str.");
var array_cat_id = new Array(".$category_id_str.");
var array_cat_title = new Array(".$cattitle_str.");
var array_k2cat_id = new Array(".$k2_id_str.");
var array_k2cat_title = new Array(".$k2cattitle_str.");
var titlePos = '".$titlePos."'; 
function loadTitlePosition(style,isFirstLoad){ 
var titlePosition = $('jform_params_title_position');       		
$('jform_params_title_position').empty();
if(style !='st_left_vertical'){
var opt       = document.createElement('option');
opt.value     = 'top';
opt.innerHTML = 'Top';
if(isFirstLoad == 1 && opt.value == titlePos) opt.selected = true;
titlePosition.appendChild(opt);
var opt       = document.createElement('option');
opt.value     = 'bottom';
opt.innerHTML = 'Bottom';
if(isFirstLoad == 1 && opt.value == titlePos) opt.selected = true;
titlePosition.appendChild(opt);
}else {
var opt       = document.createElement('option');
opt.value     = 'left';
opt.innerHTML = 'Left';
if(isFirstLoad == 1 && opt.value == titlePos) opt.selected = true;
titlePosition.appendChild(opt);
var opt       = document.createElement('option');
opt.value     = 'right';
opt.innerHTML = 'Right';
if(isFirstLoad == 1 && opt.value == titlePos) opt.selected = true;
titlePosition.appendChild(opt);
}
}       	   	
function addSelection(){				
var select_box = $('jformparamsst_selection');
var selected_box = $('st_selection_selected');
for(i=0;i<select_box.options.length;i++){
if(select_box.options[i].selected){
var opt       = document.createElement('option');
opt.value     = select_box.options[i].value;
opt.innerHTML = select_box.options[i].innerHTML;
selected_box.appendChild(opt);
select_box.removeChild(select_box.options[i]);
}
}
save_selection_list();				
}
function removeSelection(){
var type = $('jformparamstype').value;				
var selected_box = $('st_selection_selected');
for (var i=selected_box.options.length-1; i >= 0;i--) {
if (selected_box.options[i].selected) {
selected_box.removeChild(selected_box.options[i]);
}					
}
save_selection_list();
if(type=='moduleID') reload_modulelist($('jformparamsmoduleID-position').value);
if(type=='categoryID') reload_category();
if(type=='k2_content') reload_k2category();
}			
function addCategory(){
var select_box = $('jformparamsst_selection');
var selected_box = $('st_selection_selected');
for(i=0;i<select_box.options.length;i++){
if(select_box.options[i].selected){
var opt       = document.createElement('option');
opt.value     = select_box.options[i].value;
opt.innerHTML = select_box.options[i].innerHTML;
selected_box.appendChild(opt);
select_box.removeChild(select_box.options[i]);
}
}
save_selection_list();
}			
function save_selection_list(){				
var selected_box = $('st_selection_selected');
var selection_hidden_list = $('st_hid_selection');
selection_hidden_list.value = '';				
for(i=0;i<selected_box.options.length;i++){
if(selection_hidden_list.value == ''){
selection_hidden_list.value = selected_box.options[i].value;
} else {
selection_hidden_list.value+= ',' +selected_box.options[i].value;
}			
}
}
function reload_modulelist(position){
var select_box = $('jformparamsst_selection');
var selected_box = $('st_selection_selected');
$('jformparamsst_selection').empty();				
var list_selection = $('st_hid_selection').value.split(',');	
$('jformparamsst_selection').empty();
$('jformparamsst_selection').length =0;				
$('st_selection_selected').options.length =0;
for(i=0;i<list_selection.length;i++){
for(j=0;j<array_cat_id.length;j++){
if(list_selection[i] == array_cat_id[j]){
var opt       = document.createElement('option');							
opt.value     = list_selection[i];
opt.innerHTML = array_cat_title[j];
$('st_selection_selected').appendChild(opt);
}
}
for(j=0;j<array_mod_id.length;j++){
if(list_selection[i] == array_mod_id[j]){
var opt       = document.createElement('option');							
opt.value     = list_selection[i];
opt.innerHTML = array_mod_position[j]+'-'+array_mod_title[j];
$('st_selection_selected').appendChild(opt);
}
}
for(j=0;j<array_k2cat_id.length;j++){
if(list_selection[i] == array_k2cat_id[j]){
var opt       = document.createElement('option');							
opt.value     = list_selection[i];
opt.innerHTML = array_k2cat_title[j];
$('st_selection_selected').appendChild(opt);
}
}
}
//If module,append child to select box
for(var i=0; i<array_mod_id.length;i++){
var isSelected=false;					
for(var j=0; j<list_selection.length; j++){						
if (list_selection[j] == array_mod_id[i]){
isSelected=true;
break;
}
}
if (array_mod_position[i]==position){						
if (isSelected==false){
var opt       = document.createElement('option');							
opt.value     = array_mod_id[i];
opt.innerHTML = array_mod_title[i];
select_box.appendChild(opt);
}
}
}
//End module				
}	
function reload_category(){
var moduleList = $('jform_params_categoryID-list');
var position = $('jformparamsmoduleID-position').value;				
var list_selection = $('st_hid_selection').value.split(',');
var select_box = $('jformparamsst_selection');
var selected_box = $('st_selection_selected');	
$('jformparamsst_selection').empty();
$('jformparamsst_selection').length =0;				
$('st_selection_selected').options.length =0;
for(i=0;i<list_selection.length;i++){
for(j=0;j<array_cat_id.length;j++){
if(list_selection[i] == array_cat_id[j]){
var opt       = document.createElement('option');							
opt.value     = list_selection[i];
opt.innerHTML = array_cat_title[j];
$('st_selection_selected').appendChild(opt);
}
}
for(j=0;j<array_mod_id.length;j++){
if(list_selection[i] == array_mod_id[j]){
var opt       = document.createElement('option');							
opt.value     = list_selection[i];
opt.innerHTML = array_mod_position[j]+'-'+array_mod_title[j];
$('st_selection_selected').appendChild(opt);
}
}
for(j=0;j<array_k2cat_id.length;j++){
if(list_selection[i] == array_k2cat_id[j]){
var opt       = document.createElement('option');							
opt.value     = list_selection[i];
opt.innerHTML = array_k2cat_title[j];
$('st_selection_selected').appendChild(opt);
}
}
}
//Category
for(i=0;i<array_cat_id.length;i++){
var isSelected=false;
for(var j=0; j<list_selection.length; j++){						
if (list_selection[j] == array_cat_id[i]){
isSelected=true;
break;
}
}
if(!isSelected){
var opt       = document.createElement('option');
opt.value     = array_cat_id[i];
opt.innerHTML = array_cat_title[i];
$('jformparamsst_selection').appendChild(opt);
} 
}
//End category				
}
function reload_k2category(){
var moduleList = $('jform_params_categoryID-list');
var position = $('jformparamsmoduleID-position').value;				
var list_selection = $('st_hid_selection').value.split(','); 
var select_box = $('jformparamsst_selection');
var selected_box = $('st_selection_selected');	
$('jformparamsst_selection').empty();
$('jformparamsst_selection').length =0;				
$('st_selection_selected').options.length =0;	
for(i=0;i<list_selection.length;i++){
for(j=0;j<array_cat_id.length;j++){
if(list_selection[i] == array_cat_id[j]){
var opt       = document.createElement('option');							
opt.value     = list_selection[i];
opt.innerHTML = array_cat_title[j];
$('st_selection_selected').appendChild(opt);
}
}
for(j=0;j<array_mod_id.length;j++){
if(list_selection[i] == array_mod_id[j]){
var opt       = document.createElement('option');							
opt.value     = list_selection[i];
opt.innerHTML = array_mod_position[j]+'-'+array_mod_title[j];
$('st_selection_selected').appendChild(opt);
}
}
for(j=0;j<array_k2cat_id.length;j++){
if(list_selection[i] == array_k2cat_id[j]){
var opt       = document.createElement('option');							
opt.value     = list_selection[i];
opt.innerHTML = array_k2cat_title[j];
$('st_selection_selected').appendChild(opt);
}
}
}
//K2 component								
for(i=0;i<array_k2cat_id.length;i++){
var isSelected=false;
for(var j=0; j<list_selection.length; j++){						
if (list_selection[j] ==array_k2cat_id[i]){
isSelected=true;
break;
}
}
if(!isSelected){
var opt       = document.createElement('option');
opt.value     = array_k2cat_id[i];
opt.innerHTML = array_k2cat_title[i];
$('jformparamsst_selection').appendChild(opt);
} 
}
//End k2			
}
function reload_selection(){
var type = $('jformparamstype').value;
switch(type){
case 'moduleID':
var pos_select_box = $('jformparamsmoduleID-position').value;
reload_modulelist(pos_select_box);
break;
case 'categoryID':
reload_category();
break; 
case 'k2_content':
reload_k2category();
break;
}
}
function moveUp(element) {
for(i = 0; i < element.options.length; i++) {
if(element.options[i].selected == true) {
if(i != 0) {
var temp = new Option(element.options[i-1].text,element.options[i-1].value);
var temp2 = new Option(element.options[i].text,element.options[i].value);
element.options[i-1] = temp2;
element.options[i-1].selected = true;
element.options[i] = temp;
}
}
}
}
function moveDown(element) {
for(i = (element.options.length - 1); i >= 0; i--) {
if(element.options[i].selected == true) {
if(i != (element.options.length - 1)) {
var temp = new Option(element.options[i+1].text,element.options[i+1].value);
var temp2 = new Option(element.options[i].text,element.options[i].value);
element.options[i+1] = temp2;
element.options[i+1].selected = true;
element.options[i] = temp;
}
}
}
}
function updateList(){
var list = $('st_selection_selected');
var textBox = $('st_hid_selection');
textBox.value = '';
for(i = 0; i < list.options.length; i++) {
if (i == 0) {
textBox.value += list.options[i].value;
} else {
textBox.value += ',' + list.options[i].value;
}
}
}			
function change_position(value) {
reload_modulelist(value);					   
}	
window.addEvent('load',function(){				
var jpaneAutoHeight = function(){
$$('.pane-slider')[0].setStyle('height','auto');					
};				
var selectType = function(type){
switch(type){
case 'moduleID':						
var pos_select_box = $('jformparamsmoduleID-position').value;
reload_modulelist(pos_select_box);	
break;
case 'categoryID':													
reload_category();//Reload select box		
break;
case 'k2_content':							
reload_k2category();//Reload select box
break;	
}
};
var selectViewContent = function(type){
switch(type){
case 'word_count':
$('jform_params_intro_length').getParent().getParent().setStyle('display','');
break;
case 'introtext':
$('jform_params_intro_length').getParent().getParent().setStyle('display','none');
break;	
case 'fulltext':
$('jform_params_intro_length').getParent().getParent().setStyle('display','none');
break;
}
};
selectViewContent('".$viewContent."');
selectType('".$sjType."');		
loadTitlePosition('".$style."',1);			
setTimeout(jpaneAutoHeight,400);	
$('jform_params_categoryID_view').addEvent('change',function(){
selectViewContent(this.value);
});
$('jform_params_st_tabs_style').addEvent('change',function(){
loadTitlePosition(this.value,0);
});
$('jformparamstype').addEvent('change',function(){
selectType(this.value);                
});					
reload_selection();
$('st_up_arrow').addEvent('click',function(){
moveUp($('st_selection_selected'));
updateList();					
});	
$('st_down_arrow').addEvent('click',function(){
moveDown($('st_selection_selected'));
updateList();					
});	
});			
</script>
";	  
$html_return.="<input type='hidden' name='jform[params][st_selection]' id='st_hid_selection' value='".$value."' />";	 

return $html_return;
}

}
